<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教练名称" prop="no">
        <el-input v-model="queryParams.name" placeholder="请输入教练名称" clearable style="width: 150px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="机构名称" prop="no">
        <el-select v-model="queryParams.institutionId" placeholder="请选择机构">
          <el-option v-for="item in mtInstitutionList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['merchant:index']">添加教练</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange"
      :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column label="ID" prop="id" width="55" />
      <el-table-column label="教练名称" align="center" prop="name" />
      <el-table-column label="形象照" align="center" width="200">
        <template slot-scope="scope">
          <img class="list-qr" :src="imagePath + scope.row.photo">
        </template>
      </el-table-column>
      <el-table-column label="教练风格" align="center" prop="style" />
      <!-- <el-table-column label="介绍" align="center" prop="introduce" /> -->
      <!-- <el-table-column label="机构简介" align="center" prop="intro" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime"/> -->
      <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom"
        :sort-orders="['descending', 'ascending']" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="A"
            inactive-value="N"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
        </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['merchant:index']"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" v-hasPermi="['merchant:index']"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" class="common-dialog" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-row>
          <el-col :span="24">
            <el-form-item label="教练名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入教练名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="9">
            <el-form-item label="形象照" prop="photo">
              <el-upload :action="uploadAction" list-type="picture-card" :class="{ hide: hideUpload }"
                :file-list="uploadFiles" :auto-upload="true" :show-file-list="false" :headers="uploadHeader"
                :on-success="handleUploadSuccess">
                <img v-if="form.photo" :src="imagePath + form.photo" class="list-img" />
                <i v-if="!form.photo" class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <!-- <p class="form-tips">（提示：点击图片修改）</p> -->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="教练风格" prop="style">
              <el-input v-model="form.style" placeholder="请输入教练风格" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="培训机构" prop="organization">
              <el-select v-model="form.institutionId" placeholder="请选择培训机构">
                <el-option v-for="item in mtInstitutionList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="介绍">
              <editor v-model="form.introduce" :min-height="300" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { getToken } from '@/utils/auth';
import { getMtcoachList, saveSetting, update, deleteMtcoach, getMtCoachInfor } from "@/api/mtcoach";
export default {
  name: "MtcoachList",
  // components: { txMap },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 标题
      title: "",
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 搜索地点结果
      locationList: [],
      // 选中地址id
      locationValue: null,
      // 选中地址详情
      locationItem: {},
      // 默认排序
      defaultSort: { prop: 'createTime', order: 'descending' },
      // 商户类型
      typeOptions: [],
      //机构名称
      mtInstitutionList: [],
      // 表单参数
      form: {},
      // 上传地址
      uploadAction: process.env.VUE_APP_SERVER_URL + 'backendApi/file/upload',
      // 隐藏上传
      hideUpload: false,
      // 上传文件列表
      uploadFiles: [],
      uploadHeader: { 'Access-Token': getToken() },
      // 图片根目录
      imagePath: "",
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        mtInstitutionId: '',
        name: '',
      },
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询日志
    getList() {
      this.loading = true;
      getMtcoachList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.data.mtCoachList.content;
        console.log(this.list, "this.list")
        this.total = response.data.totalElements;
        this.imagePath = response.data.imagePath;
        this.typeOptions = response.data.typeList;
        this.mtInstitutionList = response.data.mtInstitutionList;
        this.loading = false;
      });
    },
    // parseTime(createTimeArray){
    //   const createTime = new Date(createTimeArray[0], createTimeArray[1] - 1, createTimeArray[2], createTimeArray[3], createTimeArray[4], createTimeArray[5]);
    //   return createTime.toLocaleString();
    // },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    //   // 点击地图修改标记
    // clickMap(evt) {
    //   console.log(evt, "子传父");
    //   // 经度
    //   this.form.longitude = evt.latLng.lng;
    //   // 维度
    //   this.form.latitude = evt.latLng.lat;
    //   if (evt.poi.name) {
    //     this.form.locationName = evt.poi.name;
    //     this.locationValue = evt.poi.name;
    //   }
    // },
    //   // 选择地址
    // changeAddress(id) {
    //   this.locationList.forEach((item) => {
    //     if (item.id == id) {
    //       this.locationItem = item;
    //     }
    //   });
    //   this.form.locationName = this.locationItem.title;
    //   this.form.managementPhone = this.locationItem.address;

    //   // console.log(id, "选中地址");
    //   // console.log(this.locationList, "locationList");
    //   // console.log(this.locationValue, "locationValue");
    //   console.log(this.locationItem, "locationItem");

    //   // 经度
    //   this.form.latitude = this.locationItem.location.lat;
    //   // 纬度
    //   this.form.longitude = this.locationItem.location.lng;
    //   // // // 调用地图组件传入经纬度,设置地图显示区域
    //   this.$refs["txMap"].setMap(this.form.longitude, this.form.latitude);
    //   // // 默认Marker为选中地址
    //   this.$refs["txMap"].setMapMarker(this.form.longitude, this.form.latitude);
    // },
    // // 地图组件给表单传搜索结果数据
    // postlocation(res) {
    //   this.locationList = res.data;
    //   console.log(this.locationList, "搜索结果");
    // },
    // // 模糊查询方法
    // remoteMethod(query) {
    //   if (query !== "") {
    //     this.query = query;
    //     // console.log(this.query,'父query');
    //     this.$refs["txMap"].getLal(query);
    //   } else {
    //     this.locationList = [];
    //   }
    // },
    //  // 设置地图
    //  setMap(placeName, longitude, latitude) {
    //   this.locationValue = placeName;
    //   this.$refs["txMap"].setMap(longitude, latitude);
    //   this.$refs["txMap"].setMapMarker(longitude, latitude);
    // },
    // // 重置地图
    // resetMap() {
    //   // // 调用地图组件传入经纬度,设置地图显示区域
    //   this.$refs["txMap"].setMap(104.244104, 30.589892);
    //   // 默认Marker为选中地址
    //   this.$refs["txMap"].setMapMarker(104.244104, 30.589892);
    //   this.locationValue = "";
    // },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 状态修改
    // handleStatusChange(row) {
    //   let text = row.status == "1" ? "启用" : "禁用";
    //   this.$modal.confirm('确认要' + text + '"' + row.name + '"商户吗？').then(function() {
    //      return updateMerchantStatus(row.id, row.status);
    //   }).then(() => {
    //      this.$modal.msgSuccess(text + "成功");
    //   }).catch(function() {
    //      row.status = row.status === "A" ? "A" : "D";
    //   });
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    // 排序触发事件
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增培训机构";
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        name: "",
        address: "",
        phone: "",
        intro: "",
        image: "",
        coverImage: "",
        createTime: ""
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 提交按钮
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            saveSetting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 修改按钮操作
    handleUpdate(row) {
      const app = this;
      app.reset();
      const id = row.id || this.ids;
      getMtCoachInfor(id).then(response => {
        app.form = response.data.mtCoach;
        app.open = true;
        app.title = "编辑商户";
      });
    },
    // 删除按钮操作
    handleDelete(row) {
      const name = row.name || this.id;
      this.$modal.confirm('是否确认删除"' + name + '"的数据项？').then(function () {
        return deleteMtcoach(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleUploadSuccess(file) {
      this.form.logo = file.data.fileName;
    }
  }
};
</script>
<style scoped>
.common-dialog>>>.el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
::v-deep  .el-input--medium .el-input__inner{
    width: 364px;
  }
</style>
  
  